package com.hdax.dm.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.common.code.CodeEnum;
import com.hdax.dm.common.exception.DmException;
import com.hdax.dm.entity.user.User;
import com.hdax.dm.user.mappers.UserMapper;
import com.hdax.dm.user.service.UserService;
import com.hdax.dm.user.vo.UserVo;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @SneakyThrows
    @Override
    public User user(String phone) {
        if(ObjectUtils.isEmpty(phone)){
            throw new DmException(CodeEnum.USER_VCODE_NOT_EXISTS);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User user = getOne(queryWrapper);
        if(user == null){
//            //注册个新账号
//            //生成八位随机数
//            String num="0123465789";
//
//            StringBuffer vcode=new StringBuffer();
//            for (int i = 1; i <= 8; i++) {
//                char n=num.charAt((int)(Math.random() * num.length()));
//                vcode.append(n);
//            }
//            User user1=new User();
//            user1.setPhone(phone);
//            user1.setNickName(vcode.toString());
//            save(user1);
        }
        return user;

    }

    @Override
    public boolean modifyPersonInfo(Long userId, UserVo vo) {
        UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
        userUpdateWrapper.set("id",userId);
        userUpdateWrapper.set("phone",vo.getPhone());
        userUpdateWrapper.set("nickName",vo.getNickName());
        userUpdateWrapper.set("realName",vo.getRealName());
        userUpdateWrapper.set("sex",vo.getSex());
        userUpdateWrapper.set("birthday",vo.getBirthday());
        userUpdateWrapper.set("idCard",vo.getIdCard());
        userUpdateWrapper.eq("id",userId);
        return  update(userUpdateWrapper);
    }
}
